package com.lanou3g.HW_0521.listener;

import com.lanou3g.HW_0521.pojo.User;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Set;

public class SessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

        // 相当于 退出登入
        HttpSession session = se.getSession();
        String username = ((User) session.getAttribute("user")).getUsername();
        ServletContext app = session.getServletContext();
        Set<String> loginUsers = (Set<String>) app.getAttribute("loginUsers");
        loginUsers.remove(username);
    }
}
